import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    uploadedImage: null,
    resultImage: null,
    loading: false
  },
  mutations: {
    setUploadedImage(state, image) {
      state.uploadedImage = image
    },
    setResultImage(state, image) {
      state.resultImage = image
    },
    setLoading(state, status) {
      state.loading = status
    }
  },
  actions: {
    async uploadImage({ commit }, { image, params }) {
      commit('setLoading', true)
      
      try {
        const formData = new FormData()
        formData.append('image', image)
        
        // 添加所有参数到formData
        for (const key in params) {
          formData.append(key, params[key])
        }
        
        const response = await Vue.prototype.$http.post('/api/upload', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        
        if (response.data.success) {
          commit('setUploadedImage', response.data.original_image)
          commit('setResultImage', response.data.result_image)
        }
        
        return response.data
      } catch (error) {
        console.error('上传图片失败:', error)
        throw error
      } finally {
        commit('setLoading', false)
      }
    }
  },
  modules: {
  }
}) 